package com.zstk.frame.config.interceptor;

import org.apache.commons.lang3.StringUtils;

/**
 * <p>
 * mybatisl拦截器公共常量
 * </p>
 *
 * @author zxt
 * @since 2022/7/4 15:06
 */
public final class MybatisSqlConstant {
    private MybatisSqlConstant() {
    }

    /** mysql */
    public static final String MYSQL = "mysql";

    /** oracle */
    public static final String ORACLE = "oracle";


    /**
     * SQL语句占位符分隔
     */
    public final static String SQL_PLACEHOLDER_REGEX = "\\?";

    /**
     * SQL语句需要转义的关键字
     */
    public final static String ESCAPE_CHAR = "\\";

    /**
     * mybatis-plus中参数前缀
     */
    public final static String MYBATIS_PLUS_WRAPPER_PREFIX = "ew.paramNameValuePairs.";

    /**
     * mybatis-plus中参数键
     */
    public final static String MYBATIS_PLUS_WRAPPER_KEY = "ew";

    /**
     * mybatis-plus中参数分隔符
     */
    public final static String MYBATIS_PLUS_WRAPPER_SEPARATOR = ".";

    /**
     * mybatis-plus中参数分隔符替换器
     */
    public final static String MYBATIS_PLUS_WRAPPER_SEPARATOR_REGEX = "\\.";

    /**
     * 已经替换过的标记
     */
    public final static String REPLACED_LIKE_KEYWORD_MARK = "replaced.keyword";


    public static boolean hasWrapper(String field) {
        if (StringUtils.isBlank(field)) {
            return false;
        }
        return field.contains(MybatisSqlConstant.MYBATIS_PLUS_WRAPPER_PREFIX);
    }

    /**
     * 判断是否是级联属性
     *
     * @param field 字段名
     *
     * @return true/false
     */
    public static boolean cascade(String field) {
        if (StringUtils.isBlank(field)) {
            return false;
        }
        return field.contains(MybatisSqlConstant.MYBATIS_PLUS_WRAPPER_SEPARATOR) && !hasWrapper(field);
    }

}
